GBML manual / 3D

Enemies and items in 3D

How to put enemies, items and bolts in the 3D view: things drawn standing in the room, hidden behind walls, with a line of sight, a shot that finds the first target, and sliding doors.

Things

An enemy or an item in the 3D view is a thing: a picture drawn standing on the floor, always turned to face the camera, smaller the farther away it is. It is not a sprite; it is drawn into the same picture as the walls, which is why a wall in front of it hides it exactly, column by column, and a nearer thing stands in front of a farther one.

An enemy is still an ordinary object. Its x and y are room pixels, so it moves, collides and measures distances as in any room seen from above, and its Draw event puts it on the screen:

// Create event of the object that starts the view
view3d_start(ts_walls);
view3d_thing(0, ts_drone, 32);   // thing 0: the drone's frames, 32 rows tall each
view3d_thing(1, ts_pickups, 16); // thing 1: item frames, 16 rows tall
// Draw event of a drone: thing 0, frame 0 to 3 in turn
view3d_draw_self(0, (game_frame() div 8) mod 4);

The pictures

A thing's picture is one tile set in your Tilesets (a picture whose width and height are multiples of 8) with the frames stacked top to bottom: a picture 32 pixels wide and 128 rows tall, with a frame height of 32, is four frames of 32 by 32. The frame height is given in view3d_thing. Color 0 is clear (or magenta, if your pictures are not in the 256-color rule); the colors are the same set as the wall pictures use.

How big

One picture pixel is 1/64 of a wall's height, so the size of the picture is the size of the thing: a picture 64 rows tall is as tall as a wall, 32 rows half a wall, 16 rows a quarter. The bottom row of the picture stands on the floor. A thing drawn with view3d_draw_thing can be lifted off the floor, in picture pixels, for a bolt or a bat.

What hides it

A wall, or a door that has not slid open, hides a thing in the columns it covers. A thing is not hidden by another thing, only drawn behind it when it is farther. A thing that is behind the camera, closer than a tenth of a tile, or so far that it would be smaller than a pixel, is not drawn.

The view draws at most 24 things a frame: the first 24 asked for. A frame with many things costs more: see Speed: why 30 frames.

Seeing and shooting

if (action_pressed(action1)) {
    var target = view3d_shoot(view3d_angle(), 200, obj_drone);
    if (instance_exists(target)) { instance_destroy(target); }
}

Doors

view3d_door turns a wall tile into a door that slides sideways when asked to open (and shut again). A door keeps its wall picture, so paint the tile with the door's picture number. While it is not open it blocks the camera, view3d_move, rays, shots and lines of sight; once it has slid three quarters of the way, everything passes. view3d_door_open says how far open it is.

Compared with the perspective floor

The perspective floor draws a flat picture running away to a horizon, and puts real sprites on it. The 3D view draws walls from the room's tiles and puts things in it. Both use the same idea of a thing shrinking with its distance, and a thing here is not a sprite: it uses none of the console's sprite memory, so the sprites stay free for a HUD and a weapon.

See also: 3D viewMaking 3D wallsCollision in 3Dview3d_thingview3d_draw_selfview3d_draw_thingview3d_line_clearview3d_shootview3d_doorview3d_door_openPerspective floorSpeed: why 30 frames